/**
 * Copyright (C) 2011 The Android Open Source Project 
 * Emial : guoyil199@163.com
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 */ 
package com.example.app.activity;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
//import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.support.v7.widget.Toolbar.OnMenuItemClickListener;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebSettings.RenderPriority;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
import android.widget.ProgressBar;

import com.example.app.R;




/**
 * @author Administrator
 * @Email guoyil199@163.com
 * @version V1.0
 * @Date 2015年8月14日 上午8:41:27
 * @Title <B>todo</B>
 * <pre>todo</pre>
 */

public class WebViewActivity extends  ActionBarActivity/*AppCompatActivity*/ {
	Toolbar toolbar;
	WebView webView;
	private static final String url="http://zuimeia.com/community/app/hot/?platform=2";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// 不明确进度条
		// requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
		// setContentView(id)
		// setProgressBarIndeterminateVisibility(true);
		// 明确进度条
		//requestWindowFeature(Window.FEATURE_PROGRESS);
		
		super.onCreate(savedInstanceState);
		setupToolBar();
		toolbar.setTitle("Html5");
		webView=new WebView(getBaseContext());
		FrameLayout.LayoutParams contentParam=new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
		contentParam.topMargin=72;
		contentParam.gravity=Gravity.TOP;
		addContentView(webView, contentParam);
		setupWebView(webView);
		webView.loadUrl(url);
	}
	
	public void setupToolBar(){
		toolbar=(Toolbar)View.inflate(getBaseContext(), R.layout.toolbar, null);
		FrameLayout.LayoutParams toolParam=new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, 72);
		toolParam.gravity=Gravity.TOP;
		addContentView(toolbar,toolParam);
		setSupportActionBar(toolbar);
		toolbar.setBackgroundColor(Color.GREEN);
		//Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
		//toolbar.setTitle(R.string.app_name);
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		toolbar.setOnMenuItemClickListener(new OnMenuItemClickListener() {
			@Override
			public boolean onMenuItemClick(MenuItem paramMenuItem) {
				return onOptionsItemSelected(paramMenuItem);
			}
		});
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if(item.getItemId()==android.R.id.home){
			if(webView.canGoBack()){
				webView.goBack();
			}else{
				onBackPressed();	
			}
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	@SuppressWarnings("deprecation")
	@SuppressLint("SetJavaScriptEnabled")
	@TargetApi(Build.VERSION_CODES.KITKAT)
	private void setupWebView(WebView webView) {
		final ProgressBar progressbar = new ProgressBar(webView.getContext(), null, android.R.attr.progressBarStyleHorizontal);
	    progressbar.setLayoutParams(new WebView.LayoutParams(WebView.LayoutParams.FILL_PARENT, 3, 0, 0));
	    webView.addView(progressbar);
		
		webView.setBackgroundColor(0);
		webView.requestFocus();
		webView.requestFocusFromTouch();
		webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
		// webView.setScrollBarSize(0);
		// webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
		WebSettings webSet = webView.getSettings();
		webSet.setJavaScriptEnabled(true);
		webSet.setJavaScriptCanOpenWindowsAutomatically(true);
		webSet.setRenderPriority(RenderPriority.HIGH);
		// webSet.setAppCacheEnabled(true);
		// webSet.setDisplayZoomControls(false);
		// webSet.setSupportZoom(false);
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
			WebView.setWebContentsDebuggingEnabled(true);
		}
		webView.setWebViewClient(new WebViewClient() {
			
			
			/*
			 * (non-Javadoc)
			 * @see android.webkit.WebViewClient#shouldOverrideUrlLoading(android.webkit.WebView, java.lang.String)
			 */
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				return super.shouldOverrideUrlLoading(view, url);
			}

			/* (non-Javadoc)
			 * @see android.webkit.WebViewClient#onReceivedError(android.webkit.WebView, int, java.lang.String, java.lang.String)
			 */
			@Override
			public void onReceivedError(WebView view, int errorCode,
					String description, String failingUrl) {
				view.loadUrl("file:///android_asset/error.html");
			}

		});
		webView.setWebChromeClient(new WebChromeClient() {
			
			
			/* (non-Javadoc)
			 * @see android.webkit.WebChromeClient#onReceivedTitle(android.webkit.WebView, java.lang.String)
			 * <pre>todo</pre>
			 */
			@Override
			public void onReceivedTitle(WebView view, String title) {
				super.onReceivedTitle(view, title);
				toolbar.setTitle(title);
			}

			/*
			 * (non-Javadoc)
			 * @see android.webkit.WebChromeClient#onProgressChanged(android.webkit.WebView, int)
			 */
			@Override
			public void onProgressChanged(WebView view, int newProgress) {
				setProgress(newProgress * 100);
				if (newProgress == 100) {
	                progressbar.setVisibility(View.GONE);
	            } else {
	                if (progressbar.getVisibility() == View.GONE)
	                    progressbar.setVisibility(View.VISIBLE);
	                progressbar.setProgress(newProgress);
	            }
	            super.onProgressChanged(view, newProgress);
			}
		});
	}
	
}
